package middol.sys

import grails.converters.JSON
import grails.gorm.transactions.Transactional
import groovy.transform.CompileStatic
import middol.result.ResultUtils
import middol.utils.DomainUtils
import net.sf.ehcache.search.expression.Or

/**
* @Description:    组织结构服务
* @Author:         zhangjiayu@middol.com
* @CreateDate:     2020/10/28 10:53
* @Version:        1.0
*/
class OrgService {
    OrgDataService orgDataService

    /**
     * 获取组织结构层级
     * @return
     */
    def getOrgStructure(def record) {
        def list = orgDataService.list([sort: "orderNum"])
        def parentOrg = null
        if(record?.orgId){
            parentOrg = orgDataService.get(record?.orgId)
        }
        def orgList = getOrgChildren(DomainUtils.domainListToMapList(list), parentOrg)
        return ResultUtils.ok(orgList)
    }

    /**
     * 获取所有子部门信息
     * @param orgList
     * @param org
     * @return
     */
    def getOrgChildren(List orgList, Object org = null){
//        def result = []
        def newOrgList = orgList.findAll {Map it ->
            it.parentOrg?.id == org?.id
        }
        newOrgList.each {
            it.children = getOrgChildren(orgList, it)
        }
        return newOrgList
    }
}
